package com.cxb.servicemap.service;

import com.cxb.internalcommon.constant.AmapConfigConstants;
import com.cxb.internalcommon.constant.CommonStatusEnum;
import com.cxb.internalcommon.dto.DicDistrict;
import com.cxb.internalcommon.dto.ResponseResult;
import com.cxb.servicemap.mapper.DicDistrictMapper;
import com.cxb.servicemap.remote.MapDicDistrictClient;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.jsqlparser.statement.select.Distinct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.xml.ws.Response;

@Service
public class DicDistrictService {

    @Autowired
    MapDicDistrictClient mapDicDistrictClient;

    @Autowired
    private DicDistrictMapper dicDistrictMapper;
    public  ResponseResult initDicDistrict(String keywords){

        String dicDistrictResult = mapDicDistrictClient.initDicDistrict(keywords);
        System.out.println(dicDistrictResult);

        //解析body
        JSONObject dicDistrictjsonObject = JSONObject.fromObject(dicDistrictResult);
        int status = dicDistrictjsonObject.getInt(AmapConfigConstants.STATUS);
        if(status!=1){
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(),CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }
        JSONArray countryJsonArray = dicDistrictjsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
        for(int country=0;country<countryJsonArray.size();country++){
            JSONObject countryJsonObject = countryJsonArray.getJSONObject(country);
            String countryAddressCode = countryJsonObject.getString(AmapConfigConstants.ADCODE);
            String countryAddressName = countryJsonObject.getString(AmapConfigConstants.NAME);
            String countryParentAddressCode = "0";
            String countryLevel = countryJsonObject.getString(AmapConfigConstants.LEVEL);
            insertDicDistrict(countryAddressCode,countryAddressName,countryLevel,countryParentAddressCode);
            
            //省
            JSONArray provinceJsonArray = countryJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            for(int province=0;province<provinceJsonArray.size();province++){
                JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(province);
                String provinceAddressCode = provinceJsonObject.getString(AmapConfigConstants.ADCODE);
                String provinceAddressName = provinceJsonObject.getString(AmapConfigConstants.NAME);
                String provinceParentAddressCode = countryAddressCode;
                String provinceLevel = provinceJsonObject.getString(AmapConfigConstants.LEVEL);
                insertDicDistrict(provinceAddressCode,provinceAddressName,provinceLevel,provinceParentAddressCode);

                //市
                JSONArray cityJsonArray = provinceJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                for(int city=0;city<cityJsonArray.size();city++){
                    JSONObject cityJsonObject = cityJsonArray.getJSONObject(city);
                    String cityAddressCode = cityJsonObject.getString(AmapConfigConstants.ADCODE);
                    String cityAddressName = cityJsonObject.getString(AmapConfigConstants.NAME);
                    String cityParentAddressCode = provinceAddressCode;
                    String cityLevel = cityJsonObject.getString(AmapConfigConstants.LEVEL);

                    insertDicDistrict(cityAddressCode,cityAddressName,cityLevel,cityParentAddressCode);

                    //区县或者镇
                    JSONArray districtArray = cityJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    for(int district=0;district< districtArray.size();district++){
                        JSONObject districtJsonObject = districtArray.getJSONObject(district);
                        String districtAddressCode = districtJsonObject.getString(AmapConfigConstants.ADCODE);
                        String districtAddressName = districtJsonObject.getString(AmapConfigConstants.NAME);
                        String districtParentAddressCode = cityAddressCode;
                        String districtLevel = districtJsonObject.getString(AmapConfigConstants.LEVEL);
                        if(districtLevel.equals(AmapConfigConstants.STREET)) {
                            continue;
                        }
                        insertDicDistrict(districtAddressCode, districtAddressName, districtLevel, districtParentAddressCode);
                    }

                }
            }
        }


        return ResponseResult.success("");
    }

    /**
     * 插入数据库
     * @param addressCode
     * @param addressName
     * @param level
     * @param parentAddressCode
     */
    public  void insertDicDistrict(String addressCode,String addressName,String level,String parentAddressCode){
        DicDistrict district = new DicDistrict();
        district.setAddressCode(addressCode);
        district.setAddressName(addressName);
        int levelInt = generateLevel(level);
        district.setLevel(levelInt);
        district.setParentAddressCode(parentAddressCode);
        //插入数据库
        dicDistrictMapper.insert(district);
    }

    /**
     *
     * @param level
     * @return
     */
    public  int generateLevel(String level){
       int levelInt = 0;
       if(level.trim().equals("country")){
           levelInt =0;
       }else if(level.trim().equals("province")){
           levelInt =1;
       }else if(level.trim().equals("city")){
           levelInt=2;
       }else if(level.trim().equals("district")){
           levelInt=3;
       }else if(level.trim().equals("street")){
           levelInt=4;
       }
       return levelInt;
    }
}
